//
//  packageViewController.m
//  Wedding fantastic
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "packageViewController.h"
#import "RecommendModel.h"
#import "PackageTableViewCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "discountDetailViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface packageViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>
// 底部scroll
@property (nonatomic, retain) UIScrollView *backScrollView;
//
@property (nonatomic, retain) CCSegmentedControl *listSegmented;
// 婚礼策划
@property (nonatomic, retain) UITableView *ceHuaTableView;
// 婚纱摄影
@property (nonatomic, retain) UITableView *sheYingTableView;
// 摄影师
@property (nonatomic, retain) UITableView *sheYingShiTable;
// 摄像师
@property (nonatomic, retain) UITableView *sheXiangTable;
// 化妆师
@property (nonatomic, retain) UITableView *huaZhuangTable;
// 主持人
@property (nonatomic, retain) UITableView *zhuChiTable;
// 接收策划数据的数组
@property (nonatomic, retain) NSMutableArray *dataArr;
// 接收摄影数据
@property (nonatomic, retain) NSMutableArray *sheYingArr;
// 接收摄影师数据
@property (nonatomic, retain) NSMutableArray *sheYingShiArr;
// 接收摄像师数据
@property (nonatomic, retain) NSMutableArray *sheXiangArr;
// 接收化妆师数据
@property (nonatomic, retain) NSMutableArray *huaZhuangArr;
// 接收主持人数据
@property (nonatomic, retain) NSMutableArray *zhuChiArr;

@property (nonatomic, assign) BOOL loading;
@property (nonatomic, assign) NSInteger page;


@end

@implementation packageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    self.navigationItem.title = @"套餐";
    self.navigationController.navigationBar.translucent = NO;
    // 设置标题颜色
    self.navigationController.navigationBar.tintColor = [UIColor brownColor];
    // 字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor brownColor],NSForegroundColorAttributeName,nil]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png" ] style:UIBarButtonItemStyleDone target:self action:@selector(leftBack:)];

    // 放segmented的
    UIScrollView *segScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 50 * HEIGHT / 667)];
    [self.view addSubview:segScroll];
    segScroll.contentSize = CGSizeMake(WIDTH * 1.35, 0);
    segScroll.pagingEnabled = NO;
    // 隐藏滚动条
    segScroll.showsHorizontalScrollIndicator = FALSE;
    [segScroll release];
    
    
    // 底部
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50 * HEIGHT / 667, WIDTH, HEIGHT)];
    [self.view addSubview:self.backScrollView];
    self.backScrollView.pagingEnabled = NO;
    self.backScrollView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 设置滚动范围
    self.backScrollView.contentSize = CGSizeMake(WIDTH * 6, 0);
    self.backScrollView.delegate = self;
    [_backScrollView release];
    self.backScrollView.pagingEnabled = YES;

    // segmentedControl
    self.listSegmented = [[CCSegmentedControl alloc] initWithItems:@[@"婚礼策划", @"婚纱摄影", @"摄影师", @"摄像师",@"化妆师",@"主持人"]];
    self.listSegmented .frame = CGRectMake(0, 0, 500 * WIDTH / 375, 50 * HEIGHT / 667);
    self.listSegmented .layer.borderWidth = 0.5;
    self.listSegmented .layer.borderColor = [[UIColor orangeColor] CGColor];
    //设置背景图片，或者设置颜色，或者使用默认白色外观
    //    listSegmented.backgroundImage = [UIImage imageNamed:@"segment_bg.png"];
    self.listSegmented .backgroundColor = [UIColor whiteColor];
    
    //阴影部分图片，不设置使用默认椭圆外观的stain
    self.listSegmented .selectedStainView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stain.png"]];
    self.listSegmented .selectedStainView = [[UIImageView alloc] init];
    self.listSegmented .selectedSegmentTextColor = [UIColor brownColor];
    self.listSegmented .segmentTextColor = [self colorWithHexString:@"#535353"];
    [self.listSegmented  addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    [segScroll addSubview:self.listSegmented ];
    
    // 策划tableView
    self.ceHuaTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0 * HEIGHT / 667, WIDTH, (HEIGHT - 64 - 80)) style:UITableViewStylePlain];
    self.ceHuaTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.ceHuaTableView.delegate = self;
    self.ceHuaTableView.dataSource = self;
    [self.backScrollView addSubview:self.ceHuaTableView];
    [_ceHuaTableView release];
    
    // 摄影tableView
    self.sheYingTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH, 0 * HEIGHT / 667, WIDTH, (HEIGHT - 64 - 80)) style:UITableViewStylePlain];
    self.sheYingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.sheYingTableView.delegate = self;
    self.sheYingTableView.dataSource = self;
    [self.backScrollView addSubview:self.sheYingTableView];
    [_sheYingTableView release];
    
    // 摄影师tableView
    self.sheYingShiTable = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH * 2, 0 * HEIGHT / 667, WIDTH, (HEIGHT - 64 - 80)) style:UITableViewStylePlain];
    self.sheYingShiTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.sheYingShiTable.delegate = self;
    self.sheYingShiTable.dataSource = self;
    [self.backScrollView addSubview:self.sheYingShiTable];
    [_sheYingShiTable release];
    
    // 摄像tableView
    self.sheXiangTable = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH * 3, 0 * HEIGHT / 667, WIDTH, (HEIGHT - 64 - 80)) style:UITableViewStylePlain];
    self.sheXiangTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.sheXiangTable.delegate = self;
    self.sheXiangTable.dataSource = self;
    [self.backScrollView addSubview:self.sheXiangTable];
    [_sheXiangTable release];
    
    // 化妆tableView
    self.huaZhuangTable = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH * 4, 0 * HEIGHT / 667, WIDTH, (HEIGHT - 64 - 80)) style:UITableViewStylePlain];
    self.huaZhuangTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.huaZhuangTable.delegate = self;
    self.huaZhuangTable.dataSource = self;
    [self.backScrollView addSubview:self.huaZhuangTable];
    [_huaZhuangTable release];
    
    // 主持人tableView
    self.zhuChiTable = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH * 5, 0 * HEIGHT / 667, WIDTH, (HEIGHT - 64 - 80)) style:UITableViewStylePlain];
    self.zhuChiTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.zhuChiTable.delegate = self;
    self.zhuChiTable.dataSource = self;
    [self.backScrollView addSubview:self.zhuChiTable];
    [_zhuChiTable release];

    // 回到顶部button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(300 * WIDTH / 375, 450 * HEIGHT / 667, 35 * WIDTH / 375, 38 * HEIGHT / 667);
    [button setBackgroundImage:[UIImage imageNamed:@"backTop"] forState:UIControlStateNormal];
    button.alpha = 0.6;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(backTop:) forControlEvents:UIControlEventTouchUpInside];

    self.page = 1;
    [self createCeHuaData];
    [self createSheYingData];
    [self createSheYingShiData];
    [self createSheXiangData];
    [self createHuaZhuangData];
    [self createZhuChiData];
    [self addHeader];
    [self addFooter];
}

// 回到顶部
- (void)backTop:(UIButton *)button
{
    [self.ceHuaTableView setContentOffset:CGPointZero animated:YES];
    [self.sheYingTableView setContentOffset:CGPointZero animated:YES];
    [self.sheYingShiTable setContentOffset:CGPointZero animated:YES];
    [self.sheXiangTable setContentOffset:CGPointZero animated:YES];
    [self.huaZhuangTable setContentOffset:CGPointZero animated:YES];
    [self.zhuChiTable setContentOffset:CGPointZero animated:YES];
}

// 刷新
- (void)addHeader
{
    __block packageViewController *guangVC = self;
    
    // 策划
    // 添加下拉刷新头部控件
    [self.ceHuaTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        guangVC.loading = NO;//标记为下拉操作
        [self.ceHuaTableView reloadData];
        [self.ceHuaTableView headerEndRefreshing];
    }];
    
    // 婚纱摄影
    // 添加下拉刷新头部控件
    [self.sheYingTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        guangVC.loading = NO;//标记为下拉操作
        [self.sheYingTableView reloadData];
        [self.sheYingTableView headerEndRefreshing];
    }];
    
    // 摄影师
    // 添加刷新头部控件
    [self.sheYingShiTable addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        guangVC.loading = NO;//标记为下拉操作
        [self.sheYingShiTable reloadData];
        [self.sheYingShiTable headerEndRefreshing];
    }];
    
    // 摄像师
    // 添加刷新头部控件
    [self.sheXiangTable addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        guangVC.loading = NO;//标记为下拉操作
        [self.sheXiangTable reloadData];
        [self.sheXiangTable headerEndRefreshing];
    }];
    
    // 化妆师
    [self.huaZhuangTable addHeaderWithCallback:^{
        guangVC.loading = NO;
        [self.huaZhuangTable reloadData];
        [self.huaZhuangTable headerEndRefreshing];
    }];
    
    // 主持人
    [self.zhuChiTable addHeaderWithCallback:^{
        guangVC.loading = NO;
        [self.zhuChiTable reloadData];
        [self.zhuChiTable headerEndRefreshing];
    }];
}

- (void)addFooter
{
    __block packageViewController *guangVC = self;
    
    // 策划
    [self.ceHuaTableView addFooterWithCallback:^{
        guangVC.loading = NO;
        [self ceHuaFooterRereshing];
    }];
    
    // 摄影
    [self.sheYingTableView addFooterWithCallback:^{
        guangVC.loading = NO;
        [self sheYingFooterRereshing];
    }];
    
    // 摄影师
    // 添加刷新底部控件
    [self.sheYingShiTable addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        guangVC.page = 1;
        guangVC.loading = NO;//标记为下拉操作
        [self sheYingShiFooterRereshing];
    }];
    
    // 摄像师
    [self.sheXiangTable addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        guangVC.page = 1;
        guangVC.loading = NO;//标记为下拉操作
        [self sheXiangShiFooterRereshing];
    }];
    
    // 化妆师
    [self.huaZhuangTable addFooterWithCallback:^{
        guangVC.loading = NO;
        [self huaZhuangFooterRereshing];
    }];
    
    // 主持人
    [self.zhuChiTable addFooterWithCallback:^{
        guangVC.loading = NO;
        [self zhuChiFooterRereshing];
    }];
}

// 策划
// 上拉
- (void)ceHuaFooterRereshing
{
    NSString *url = @"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=2&page=nnnnnn";
    NSString *pageStr = [NSString stringWithFormat:@"%ld", ++self.page];
    NSString *urlStr = [url stringByReplacingOccurrencesOfString:@"nnnnnn" withString:pageStr];
    [NetworkTool get:urlStr body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.dataArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.ceHuaTableView reloadData];
        [self.ceHuaTableView footerEndRefreshing];
        } failure:^(NSError *error) {
        
    }];
    
}
// 婚纱摄影
// 上拉
- (void)sheYingFooterRereshing
{
    NSString *url = @"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=6&page=nnnnnn";
    NSString *pageStr = [NSString stringWithFormat:@"%ld", ++self.page];
    NSString *urlStr = [url stringByReplacingOccurrencesOfString:@"nnnnnn" withString:pageStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.sheYingArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.sheYingTableView reloadData];
        [self.sheYingTableView footerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

// 摄影师
// 上拉
- (void)sheYingShiFooterRereshing
{
    NSString *url = @"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=7&page=nnnnnn";
    NSString *pageStr = [NSString stringWithFormat:@"%ld", ++self.page];
    NSString *urlStr = [url stringByReplacingOccurrencesOfString:@"nnnnnn" withString:pageStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.sheYingShiArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.sheYingShiTable reloadData];
        [self.sheYingShiTable footerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

// 摄像师
// 上拉
- (void)sheXiangShiFooterRereshing
{
    NSString *url = @"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=8&page=nnnnnn";
    NSString *pageStr = [NSString stringWithFormat:@"%ld", ++self.page];
    NSString *urlStr = [url stringByReplacingOccurrencesOfString:@"nnnnnn" withString:pageStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.sheXiangArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.sheXiangTable reloadData];
        [self.sheXiangTable footerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

// 化妆师
// 上拉
- (void)huaZhuangFooterRereshing
{
    NSString *url = @"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=9&page=nnnnnn";
    NSString *pageStr = [NSString stringWithFormat:@"%ld", ++self.page];
    NSString *urlStr = [url stringByReplacingOccurrencesOfString:@"nnnnnn" withString:pageStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.huaZhuangArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.huaZhuangTable reloadData];
        [self.huaZhuangTable footerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

// 主持人
// 上拉
- (void)zhuChiFooterRereshing
{
    
    NSString *url = @"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=11&page=nnnnnn";
    NSString *pageStr = [NSString stringWithFormat:@"%ld", ++self.page];
    NSString *urlStr = [url stringByReplacingOccurrencesOfString:@"nnnnnn" withString:pageStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.zhuChiArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.zhuChiTable reloadData];
        [self.zhuChiTable footerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


// tableview的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:self.ceHuaTableView]) {
        return self.dataArr.count;
    } else if ([tableView isEqual:self.sheYingTableView]) {
        return self.sheYingArr.count;
    } else if ([tableView isEqual:self.sheYingShiTable]) {
        return self.sheYingShiArr.count;
    } else if ([tableView isEqual:self.sheXiangTable]) {
        return self.sheXiangArr.count;
    } else if ([tableView isEqual:self.huaZhuangTable]) {
        return self.huaZhuangArr.count;
    } else {
        return self.zhuChiArr.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.ceHuaTableView]) {
        static NSString *reuse = @"reuse";
        PackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[PackageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
        }
        cell.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        RecommendModel *model = self.dataArr[indexPath.row];
        cell.titleLabel.text = model.title;
        cell.sellName.text = model.name;
        cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", model.actual_price];
        [cell.topImage sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];
        return cell;
        
    } else if ([tableView isEqual:self.sheYingTableView]) {
        static NSString *sheYing = @"sheYing";
        PackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sheYing];
        if (!cell) {
            cell = [[[PackageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:sheYing] autorelease];
        }
        cell.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        RecommendModel *model = self.sheYingArr[indexPath.row];
        cell.titleLabel.text = model.title;
        cell.sellName.text = model.name;
        cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", model.actual_price];
        [cell.topImage sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];
        return cell;
        
    } else if ([tableView isEqual:self.sheYingShiTable]) {
        static NSString *sheYingShi = @"sheYingShi";
        PackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sheYingShi];
        if (!cell) {
            cell = [[[PackageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:sheYingShi] autorelease];
        }
        cell.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        RecommendModel *model = self.sheYingShiArr[indexPath.row];
        cell.titleLabel.text = model.title;
        cell.sellName.text = model.name;
        cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", model.actual_price];
        [cell.topImage sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];
        return cell;
        
    } else if ([tableView isEqual:self.sheXiangTable]) {
        static NSString *sheXiang = @"sheXiang";
        PackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sheXiang];
        if (!cell) {
            cell = [[[PackageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:sheXiang] autorelease];
        }
        cell.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        RecommendModel *model = self.sheXiangArr[indexPath.row];
        cell.titleLabel.text = model.title;
        cell.sellName.text = model.name;
        cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", model.actual_price];
        [cell.topImage sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];

        return cell;
        
    } else if ([tableView isEqual:self.huaZhuangTable]) {
        static NSString *huaZhuang = @"huaZhuang";
        PackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:huaZhuang];
        if (!cell) {
            cell = [[[PackageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:huaZhuang] autorelease];
        }
        cell.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        RecommendModel *model = self.huaZhuangArr[indexPath.row];
        cell.titleLabel.text = model.title;
        cell.sellName.text = model.name;
        cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", model.actual_price];
        [cell.topImage sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];
        return cell;
    } else {
        static NSString *zhuChi = @"zhuChi";
        PackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:zhuChi];
        if (!cell) {
            cell = [[[PackageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:zhuChi] autorelease];
        }
        cell.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        RecommendModel *model = self.zhuChiArr[indexPath.row];
        cell.titleLabel.text = model.title;
        cell.sellName.text = model.name;
        cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", model.actual_price];
        [cell.topImage sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.ceHuaTableView]) {
        discountDetailViewController *sellVC = [[discountDetailViewController alloc] init];
        sellVC.ID = (NSString *)[self.dataArr[indexPath.row] bId];
        [self.navigationController pushViewController:sellVC animated:YES];
        [sellVC release];
    } else if ([tableView isEqual:self.sheYingTableView]) {
        discountDetailViewController *sellVC = [[discountDetailViewController alloc] init];
        sellVC.ID = (NSString *)[self.sheYingArr[indexPath.row] bId];
        [self.navigationController pushViewController:sellVC animated:YES];
        [sellVC release];
    } else if ([tableView isEqual:self.sheYingShiTable]) {
        discountDetailViewController *sellVC = [[discountDetailViewController alloc] init];
        sellVC.ID = (NSString *)[self.sheYingShiArr[indexPath.row] bId];
        [self.navigationController pushViewController:sellVC animated:YES];
        [sellVC release];
    } else if ([tableView isEqual:self.sheXiangTable]) {
        discountDetailViewController *sellVC = [[discountDetailViewController alloc] init];
        sellVC.ID = (NSString *)[self.sheXiangArr[indexPath.row] bId];
        [self.navigationController pushViewController:sellVC animated:YES];
        [sellVC release];
    } else if ([tableView isEqual:self.huaZhuangTable]) {
        discountDetailViewController *sellVC = [[discountDetailViewController alloc] init];
        sellVC.ID = (NSString *)[self.huaZhuangArr[indexPath.row] bId];
        [self.navigationController pushViewController:sellVC animated:YES];
        [sellVC release];
    } else {
        discountDetailViewController *sellVC = [[discountDetailViewController alloc] init];
        sellVC.ID = (NSString *)[self.zhuChiArr[indexPath.row] bId];
        [self.navigationController pushViewController:sellVC animated:YES];
        [sellVC release];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 280;
}

// 解析策划数据
- (void)createCeHuaData
{
    self.dataArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=2&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.dataArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.ceHuaTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

// 解析摄影数据
- (void)createSheYingData
{
    self.sheYingArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=6&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.sheYingArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        
        [self.sheYingTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

// 解析摄影师数据
- (void)createSheYingShiData
{
    self.sheYingShiArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=7&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.sheYingShiArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.sheYingShiTable reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

// 解析摄像师数据
- (void)createSheXiangData
{
    self.sheXiangArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=8&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.sheXiangArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.sheXiangTable reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

// 解析化妆师数据
- (void)createHuaZhuangData
{
    self.huaZhuangArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=9&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.huaZhuangArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.huaZhuangTable reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

// 解析主持人数据
- (void)createZhuChiData
{
    self.zhuChiArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList?city=0&sort%5Bkey%5D=default&sort%5Border%5D=desc&commodity_type=0&mark=&per_page=20&property=11&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *merchantsArr = dic[@"works"];
        for (NSDictionary *tempDic in merchantsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.zhuChiArr addObject:model];
            
            NSDictionary *merchantDic = tempDic[@"merchant"];
            [model setValuesForKeysWithDictionary:merchantDic];
            [model release];
        }
        [self.zhuChiTable reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}



// 导航控制器返回按钮点击方法
- (void)leftBack:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

// scroller结束拖拽时触发, 只有不动了才会触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 判断segment当前是谁
    if (self.backScrollView.contentOffset.x < WIDTH) {
        self.listSegmented.selectedSegmentIndex = 0;
    } else if (self.backScrollView.contentOffset.x < WIDTH * 2) {
        self.listSegmented.selectedSegmentIndex = 1;
    } else if (self.backScrollView.contentOffset.x < WIDTH * 3) {
        self.listSegmented.selectedSegmentIndex = 2;
    } else if (self.backScrollView.contentOffset.x < WIDTH * 4) {
        self.listSegmented.selectedSegmentIndex = 3;
    } else if (self.backScrollView.contentOffset.x < WIDTH * 5) {
        self.listSegmented.selectedSegmentIndex = 4;
    } else {
        self.listSegmented.selectedSegmentIndex = 5	;
    }
}

- (void)valueChanged:(id)sender
{
    CCSegmentedControl* segmentedControl = sender;
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
        {
            [self.backScrollView setContentOffset:CGPointMake(0, 0)];
        }
            break;
        case 1:
        {
            [self.backScrollView setContentOffset:CGPointMake(WIDTH, 0)];
        }
            break;
        case 2:
        {
            [self.backScrollView setContentOffset:CGPointMake(WIDTH * 2, 0)];
        }
            break;
        case 3:
        {
            [self.backScrollView setContentOffset:CGPointMake(WIDTH * 3, 0)];
        }
            break;
        case 4:
        {
            [self.backScrollView setContentOffset:CGPointMake(WIDTH * 4, 0)];
        }
            break;
        case 5:
        {
            [self.backScrollView setContentOffset:CGPointMake(WIDTH * 5, 0)];
        }
            break;
            
        default:
            break;
    }
    
}

- (UIColor *) colorWithHexString: (NSString *) hexString {
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
    CGFloat alpha, red, blue, green;
    switch ([colorString length]) {
        case 3: // #RGB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 1];
            green = [self colorComponentFrom: colorString start: 1 length: 1];
            blue  = [self colorComponentFrom: colorString start: 2 length: 1];
            break;
        case 4: // #ARGB
            alpha = [self colorComponentFrom: colorString start: 0 length: 1];
            red   = [self colorComponentFrom: colorString start: 1 length: 1];
            green = [self colorComponentFrom: colorString start: 2 length: 1];
            blue  = [self colorComponentFrom: colorString start: 3 length: 1];
            break;
        case 6: // #RRGGBB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 2];
            green = [self colorComponentFrom: colorString start: 2 length: 2];
            blue  = [self colorComponentFrom: colorString start: 4 length: 2];
            break;
        case 8: // #AARRGGBB
            alpha = [self colorComponentFrom: colorString start: 0 length: 2];
            red   = [self colorComponentFrom: colorString start: 2 length: 2];
            green = [self colorComponentFrom: colorString start: 4 length: 2];
            blue  = [self colorComponentFrom: colorString start: 6 length: 2];
            break;
        default:
            [NSException raise:@"Invalid color value" format: @"Color value %@ is invalid.  It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
            break;
    }
    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}

- (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
    unsigned hexComponent;
    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
    return hexComponent / 255.0;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
